package com.apestech.api.exp;

import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.StringUtil;

import java.util.ArrayList;

public class XLSTable extends StringUtil {

    public class Field{
        private String field;
        private String table;
        private String look;
        private JSONObject listmap;

        public Field(JSONObject map) throws Exception {
            field=map.getString("field");
            String list=map.getString("list");
            table=map.getString("table");
            look=map.getString("look");
            if(list!=null && !"".equals(list)){
                listmap=getStrSplitValueJson("; :", list);
            }
        }

        public String getField() {
            return field;
        }

        public JSONObject getListmap() {
            return listmap;
        }

        public String getTable() {
            return table;
        }

        public String getLook() {
            return look;
        }
    }

    public class Sheet{
        private JSONObject head;
        private JSONObject idhead;
        private JSONObject field;
        private String updateTable;
        private String updateTableK;
        private String conditionStrategyId;
        private String update;
        private String mode;
        private JSONObject look;
        private JSONObject updateField;
        private String name;
        private boolean truncate=false;

        public Sheet(JSONObject map) throws Exception {
            field=new JSONObject();
            if(map==null) return;
            updateTable=map.getString("updateTable");
            updateTableK=getVarKey(updateTable);
            conditionStrategyId=map.getString("conditionStrategyId");
            update=map.getString("update");
            mode=map.getString("mode");
            name=map.getString("name");
            if(name==null){
                name=map.getString("index");
            }
            updateField=(JSONObject) map.get("updateField");
            head=(JSONObject) map.get("head");
            truncate="1".equals(map.getString("truncate"));
            if(head!=null){
                idhead=new JSONObject();
                ArrayList<String> klist=head.getKeylist();
                int len=klist.size();
                for(int i=0; i<len; i++){
                    idhead.put(head.getString(klist.get(i)), klist.get(i));
                }
            }
            look=new JSONObject();
            if(map.get("fields")!=null){
                JSONArray f=new JSONArray();
                if(map.get("fields") instanceof JSONArray){
                    f= (JSONArray) map.get("fields");
                } else {
                    f.add(((JSONObject)map.get("fields")).get("field"));
                }
                int len=f.size();
                for(int i=0; i<len; i++){
                    addField(f.get(i));
                }
            }
        }

        public void addField(JSONObject map) throws Exception {
            Field f=new Field(map);
            field.put(f.getField(), f);
            if(f.getLook()!=null){
                look.put(f.getLook(),f.getField());
            }
        }

        public int size(){
            return field.size();
        }

        public JSONObject getField() {
            return field;
        }

        public JSONObject getHead() {
            return head;
        }

        public void setHead(JSONObject head) {
            this.head = head;
        }

        public JSONObject getLook() {
            return look;
        }

        public JSONObject getIdhead() {
            return idhead;
        }

        public String getUpdateTable() {
            return updateTable;
        }

        public JSONObject getUpdateField() {
            return updateField;
        }

        public String getMode() {
            return mode;
        }

        public String getName() {
            return name;
        }

        public boolean isTruncate() {
            return truncate;
        }

        public String getUpdateTableK() {
            return updateTableK;
        }
    }

    private String id;
    private JSONObject sheet;

    public XLSTable(){
        sheet=new JSONObject();
    };

    public XLSTable(JSONObject map) throws Exception {
        sheet=new JSONObject();
        id=map.getString("id");
        JSONArray sh=new JSONArray();
        if(map.get("sheet") instanceof JSONArray){
            sh= (JSONArray) map.get("sheet");
        } else {
            sh.add(map.get("sheet"));
        }
        int len=sh.size();
        for(int i=0; i<len; i++){
            addSheet(sh.get(i));
        }
    }

    public void addSheet(JSONObject map) throws Exception {
        Sheet st=new Sheet(map);
        sheet.put(st.getName(), st);
    }

    public int size(){
        return sheet.size();
    }

    public JSONObject getHead(String name){
        Sheet st= (Sheet) sheet.get(name);
        if(st==null) return null;
        return st.getHead();
    }

    public JSONObject getIdHead(String name){
        Sheet st= (Sheet) sheet.get(name);
        if(st==null) return null;
        return st.getIdhead();
    }

    public JSONObject getField(String name){
        Sheet st= (Sheet) sheet.get(name);
        if(st==null) return null;
        return st.getField();
    }

    public JSONObject getLook(String name){
        Sheet st= (Sheet) sheet.get(name);
        if(st==null) return null;
        return st.getLook();
    }

    public JSONObject getUpdateField(String name){
        Sheet st= (Sheet) sheet.get(name);
        if(st==null) return null;
        return st.getUpdateField();
    }

    public String getUpdateTable(String name){
        Sheet st= (Sheet) sheet.get(name);
        if(st==null) return null;
        return st.getUpdateTable();
    }

    public String getUpdateTableK(String name){
        Sheet st= (Sheet) sheet.get(name);
        if(st==null) return null;
        return st.getUpdateTableK();
    }

    public String getMode(String name){
        Sheet st= (Sheet) sheet.get(name);
        if(st==null) return null;
        return st.getMode();
    }

    public boolean isTruncate(String name) {
        Sheet st= (Sheet) sheet.get(name);
        if(st==null) return false;
        return st.isTruncate();
    }

    public Sheet getSheet(String name){
        return (Sheet) sheet.get(name);
    }

}
